﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;

namespace DictonaryMaker
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Welcome to dictonary maker 1.2.1");
            Console.WriteLine("If you want to create new dictonary, write DC.");
            Console.WriteLine("If you want to translate something by existing dictonary, type TR.");
            Console.WriteLine("If you want to upgrade an existing dictonary, type ADD.");
            string answer = Console.ReadLine();
            if (answer == "DC")
                CreateNewDirectory();
            else if (answer == "TR")
                Translate();
            else if (answer == "ADD")
                Add();

        }
        static void CreateNewDirectory()
        {
            Console.WriteLine("Type name of dictonary:");
            string name = Console.ReadLine();
            Console.WriteLine("Start to write a dictonary. You can exit application by writing a \"EXIT\"");
            Console.WriteLine("Write words by this form: WordInFirstLang*WordInSecondLang .");
            StreamWriter sw = new StreamWriter("C:\\Dictonaries\\" + name + ".dic");
            sw.Write("");
            sw.Close();
            while (true)
            {
                string line = Console.ReadLine();
                if (line == "EXIT")
                    Environment.Exit(1);
                else
                    WriteWords(line, name);
            }
        }
        static void WriteWords (string line, string name)
        {
            
            using (StreamWriter sw = File.AppendText("C:\\Dictonaries\\" + name + ".dic"))
            {
                sw.WriteLine(line);
            }

        }
        static void Translate()
        {
            Console.WriteLine("Type name of dictonary:");
            string name = Console.ReadLine();
            Console.WriteLine("You can exit by typing \"EXIT\"");
            while (true)
            {
                string lookingfor = Console.ReadLine();
                Console.WriteLine(Oposite(lookingfor, name));
            }
        }
        static string Oposite(string lookingfor, string name)
        {
            string retstr = "";
            StreamReader sr = new StreamReader("C:\\Dictonaries\\" + name + ".dic");
            string fullcode = sr.ReadToEnd();
            string[] lines = fullcode.Split(new string[] { "\r\n" }, StringSplitOptions.None);
            foreach (string line in lines)
            {
                if (line.Contains("*"))
                {
                    string[] words = line.Split('*');
                    if (words[0] == lookingfor)
                    {
                        retstr += words[1];
                        break;
                    }
                    else if (words[1] == lookingfor)
                    {
                        retstr += words[0];
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
                else
                    continue;
            }
            if (retstr == "")
                retstr = "Expression is not in dictonary.";
            return retstr;
        }
        static void Add()
        {
            Console.WriteLine("Type the name of dictonary what you want upgrade:");
            string name = Console.ReadLine();
            Console.WriteLine("You can exit application by typing \"EXIT\".");
            using (StreamWriter sw = File.AppendText(name))
            {
                while (true)
                {
                    string expression = Console.ReadLine();
                    if (expression == "EXIT")
                        Environment.Exit(1);
                    else
                    {
                        sw.WriteLine(expression);
                    }
                }
                sw.Close();
            }
        }
    }
}
